<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/10
 * Time: 14:53
 */
/*
 * 商场打折例子，比如一个商场，需要一个记录商品数量和总价的功能，但是可能需要多种折扣，比如 1:1--9折的情况。2：返点的情况（满300返100 满返可能也存在不同情况.满300返80）
 */

/*
 * 抽象类
 */
abstract class CashSuper{
    public abstract function acceptCash($money);
}

/*
 * 不打折的时候
 */
class CashNormal extends CashSuper{
    public function acceptCash($money)
    {
        return $money;
    }
}

/*
 * 折扣的时候
 */
class CashRebate extends CashSuper{
    private $moneyRebate = 1;

    public function __construct($moneyRebate)
    {
        $this->moneyRebate = $moneyRebate;
    }

    public function acceptCash($money)
    {
        return $money * $this->moneyRebate;
    }
}

/*
 * 返点的时候
 */
class CashReturn extends CashSuper{
    private $moneyCondition = 0;
    private $moneyReturn = 0;
    public function __construct($moneyCondition,$moneyReturn)
    {
        $this->moneyCondition = $moneyCondition;
        $this->moneyReturn = $moneyReturn;
    }

    public function acceptCash($money)
    {
        $res = $money;
        if($money > $this->moneyCondition){
            $res = $money - floor($money/$this->moneyCondition) * $this->moneyReturn;
        }
        return $res;
    }
}

/**
 * 收费工厂
 */

class CashFactory{
    public static function createCashAccept($type){
        $cashSuper = null;
        switch($type){
            case "正常收费":
                $cashSuper = new CashNormal();
                break;
            case "满300返100":
                $cashSuper = new CashReturn(300,100);
                break;
            case "打8折":
                $cashSuper = new CashRebate(0.8);
                break;
        }
        return $cashSuper;
    }
}

/**
 * 客户侧程序
 */
//$type = '正常收费';
//$cashSuper = CashFactory::createCashAccept($type);
////$num是商品数量，$price是商品价格
//$totalPrice = $cashSuper->acceptCash($num * $price);

$a = ['a'=>1,'b'=>2];
$aa = ['aa'=>1,'ab'=>2];
var_dump($a + $aa);

